-
-
Notifications
You must be signed in to change notification settings - Fork 360
Added forward euler in python3 #203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for another contribution!
First of all, you should add a code import to euler.md or else your code won't show up at all.
{% sample lang="py" %}
### Python
[import, lang:"python"](code/python/euler.py)
Then I found a few consistency things and one actual error in your code, but nothing you won't be able to fix.
def forward_euler(time_step, n): | ||
result = [0] * n | ||
result[0] = 1 | ||
for i in range(1, n): result[i] = result[i - 1] - 3.0 * result[i - 1] * time_step |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd put the loop header and the result[i] = ...
statement on two separate lines.
def forward_euler(time_step, n): | ||
result = [0] * n | ||
result[0] = 1 | ||
for i in range(1, n): result[i] = result[i - 1] - 3.0 * result[i - 1] * time_step |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why 3.0
instead of just 3
?
return approx | ||
|
||
|
||
time_step = 0.01 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most of the Python code we have in the archive uses a main()
function. To keep everything consistent, you should put this stuff inside a def main():
and call main()
like, for example, in this file.
if abs(result[i] - solution) > threshold: | ||
print(result[i], solution) | ||
approx = False | ||
return approx |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your return
statement is inside the for-loop.
That should do it! |
No description provided.